/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inputsms.utilities;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import inputsms.model.Sms;
import org.apache.log4j.Logger;

/**
 *
 * @author aisyah
 */
public class CreateFile {

    private static Logger logger = Logger.getLogger(CreateFile.class);
    private Sms smsModel;

    public CreateFile(Sms smsModel) {
        this.smsModel = smsModel;
    }

    private String getURL() {
        String URL = null;
        if (smsModel.getKey().equalsIgnoreCase(DefaultKey.REG_FB)) {
            URL = Property.getProperty("dir.process") + "0001/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_LOGIN)) {
            URL = Property.getProperty("dir.process") + "0002/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_BUDDY)) {
            URL = Property.getProperty("dir.process") + "0003/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_BUDYNEW)) {
            URL = Property.getProperty("dir.process") + "0004/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_TO)) {
            URL = Property.getProperty("dir.process") + "0005/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_LOGOUT)) {
            URL = Property.getProperty("dir.process") + "0006/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_STOP)) {
            URL = Property.getProperty("dir.process") + "0007/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_HELP)) {
            URL = Property.getProperty("dir.process") + "0008/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_HELP_MORE)) {
            URL = Property.getProperty("dir.process") + "0009/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_CHANGE)) {
            URL = Property.getProperty("dir.process") + "0010/";
        } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_STATUS)) {
            URL = Property.getProperty("dir.process") + "0011/";
        } else {
            URL = Property.getProperty("dir.fault");
        }

        return URL;
    }

    public boolean writeFile() {
        boolean result = true;
        String URL = this.getURL();

        File f = new File(URL + System.currentTimeMillis() + "-" + smsModel.getMsisdn() + ".txt");
        PrintStream p = null;

        try {
            p = new PrintStream(f);
            p.append(smsModel.getMsisdn() + "\n");
            if ((smsModel.getKey().equalsIgnoreCase(DefaultKey.REG_FB)) || smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_CHANGE)) {
                p.append(smsModel.getEmail() + "\n" + smsModel.getPassword());
            } else if (smsModel.getKey().equalsIgnoreCase(DefaultKey.FB_TO)) {
                p.append(smsModel.getBuddyId() + "\n" + smsModel.getContent());
            } else if (smsModel.getKey().equalsIgnoreCase(Property.getProperty("dir.fault"))) {
                p.append(smsModel.getContent());
                if (smsModel.getContent().equalsIgnoreCase(MessageId.COMMAND_NOT_FOUND)) {
                    p.append( "\n" + smsModel.getMsg());
                }
            } else {
                p.append((smsModel.getContent() == null) ? " " : smsModel.getContent());
            }
            p.close();
        } catch (FileNotFoundException ex) {
            logger.error("file " + Property.getProperty("dir.process") + "not found");
            result = false;
        }
        return result;
    }
}
